package com.galleryviewpager.i_wave_loading;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;


public class WaveLoadingView4 extends View {

    private static final String TAG = "WaveLoadingView4";

    private Paint mRectPaint = new Paint();

    private Paint mWavePaint = new Paint();

    private Paint mCirclePaint = new Paint();
    private RectF rect = new RectF();

    private Path wavePath = new Path();

    private int mWidth, mHeight;
    private PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    private int widthPercent = 0, heightPercent = 0;

    private int leftPercent = 0, rightPercent = 0;

    private boolean isAdd = false;
    private Bitmap bitmap;
    private Canvas mCanvas;

    public WaveLoadingView4(Context context) {
        this(context, null);
    }

    public WaveLoadingView4(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveLoadingView4(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        setLayerType(LAYER_TYPE_HARDWARE, null);
        mRectPaint.setColor(Color.RED);

        mWavePaint.setColor(Color.GREEN);
        mWavePaint.setAntiAlias(true);
        mWavePaint.setStyle(Paint.Style.FILL);

        mCirclePaint.setColor(Color.YELLOW);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        mWidth = widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : 600;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mHeight = heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec) : 600;
        setMeasuredDimension(mWidth, mHeight);
        rect.set((int) (mWidth * 0.1), (float) (mHeight * 0.1), (int) (mWidth * 0.9), (int) (mHeight * 0.9));
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(bitmap);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        wavePath.reset();

//        canvas.drawColor(Color.BLUE);
//        canvas.drawRect(rect, mRectPaint);


        float startX = (float) (mWidth * 0.1);
        int startHeight = (100 - heightPercent) * mHeight / 100;
        wavePath.moveTo(startX, startHeight);

        float x1 = (float) (mWidth * leftPercent / 100 + startX);
        float y1 = (float) (startHeight + mHeight * 0.1);

        float x2 = (float) (mWidth * leftPercent / 100 + startX);
        float y2 = (float) (startHeight - mHeight * 0.1);

        float x3 = (float) (mWidth * 0.9);
        float y3 = (float) (mHeight * 0.5);
        wavePath.cubicTo(x1, y1, x2, y2, x3, startHeight);

        float bottomY = (float) (mHeight * 0.9);
        wavePath.lineTo(x3, bottomY);
        wavePath.lineTo(startX, bottomY);
        wavePath.close();

//           //这种写法有问题 会有不明的白色条框
//        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 5 * 2, mCirclePaint);
//        mWavePaint.setXfermode(porterDuffXfermode);
//        canvas.drawPath(wavePath, mWavePaint);
//        mWavePaint.setXfermode(null);



        mCanvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 5 * 2, mCirclePaint);

        mWavePaint.setXfermode(porterDuffXfermode);
        mCanvas.drawPath(wavePath, mWavePaint);
        mWavePaint.setXfermode(null);
        canvas.drawBitmap(bitmap, 0, 0, null);

        if (heightPercent <= 100) {
            if (leftPercent > 70) {
                isAdd = false;
            } else if (leftPercent < 20) {
                isAdd = true;
            }
            if (isAdd) {
                leftPercent += 5;
            } else {
                leftPercent -= 5;
            }

            heightPercent++;
            Log.e(TAG, "onDraw: " + x1 + " 先=> " + x2);
            postInvalidateDelayed(50);
        }
    }
}
